#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>

using namespace std;

struct ListNode
{
    int val;
    string pos;
    string next;
};

ostream& operator<<(ostream& out, ListNode& x)
{
    out << x.pos << ' ' << x.val << ' ' << x.next;

    return out;
}

int main()
{
    vector<ListNode> data;
    string start;
    int n;
    cin >> start >> n;
    while (n--)
    {
        ListNode tmp;
        cin >> tmp.pos;
        cin >> tmp.val;
        cin >> tmp.next;
        data.push_back(tmp);
    }
    for (int i = 0; i < data.size(); ++i)
    {
        for (int j = i + 1; j < data.size(); ++j)
        {
            if (data[j].pos == start)
            {
                swap(data[j], data[i]);
                break;
            }
        }
        start = data[i].next;
    }
    
    vector<ListNode> reject;
    for (int i = 1; i < data.size(); ++i)
    {
        for (int j = 0; j < i; ++j)
        {
            if (abs(data[j].val) == abs(data[i].val))
            {
                reject.push_back(data[i]);
                data.erase(data.begin() + i);
            }
        }
    }
    string end = "-1";
    for (int i = 0; i < data.size() - 1; ++i)
    {
        data[i].next = data[i + 1].pos;
    }
    data[data.size() - 1].next = end;
    for (int i = 0; i < reject.size() - 1; ++i)
    {
        reject[i].next = reject[i+1].pos;
    }
    reject[reject.size()-1].next = end;

    for (auto& e : data)
    {
        cout << e << endl;
    }
    for (auto& e : reject)
    {
        cout << e << endl;
    }
    
    return 0;
}
